package GUILayer;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.FlowLayout;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.border.EtchedBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import ControllerLayer.ForestOwnerCtr;
import ModelLayer.Forest;
import ModelLayer.Owner;
import ModelLayer.Tree;

import java.awt.Font;

import javax.swing.SwingConstants;

public class StartScreen extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTextField textField_8;
	private JLabel label_2;
	private DefaultListModel<String> model;
	private DefaultListModel<String> model_2;
	private JList list;
	private JList list_2;
	private ArrayList<Forest> fList;
	private Forest f;
	private Owner owner;
	
	//Controller field
	ForestOwnerCtr ctr = new ForestOwnerCtr();
	private JTextField textField_9;
	private JTextField textField_10;
	private JTextField textField_11;
	private JTextField textField_12;
	private JTextField textField_13;
	private JTextField textField_14;
	private JTextField textField_15;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StartScreen frame = new StartScreen();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public StartScreen() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1006, 696);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel.setBounds(10, 11, 274, 380);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JButton btnCreate = new JButton("Create");
		btnCreate.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				createPerson();
			}
		});
		btnCreate.setBounds(158, 305, 89, 23);
		panel.add(btnCreate);
		
		JButton btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				deletePerson();
			}
			
		});
		btnDelete.setBounds(65, 305, 83, 23);
		panel.add(btnDelete);
		
		JButton btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearPersonTxt();
			}
		});
		btnClear.setBounds(65, 271, 83, 23);
		panel.add(btnClear);
		
		textField = new JTextField();
		textField.setBounds(93, 161, 154, 25);
		panel.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(93, 197, 154, 23);
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(93, 229, 154, 23);
		panel.add(textField_2);
		
		JLabel lblOwnerName = new JLabel("Owner Name:");
		lblOwnerName.setBounds(10, 166, 85, 14);
		panel.add(lblOwnerName);
		
		JLabel lblNewLabel = new JLabel("Address:");
		lblNewLabel.setBounds(37, 201, 58, 14);
		panel.add(lblNewLabel);
		
		JLabel lblPhoneNumber = new JLabel("Phone No.:");
		lblPhoneNumber.setBounds(26, 233, 69, 14);
		panel.add(lblPhoneNumber);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(93, 52, 154, 23);
		panel.add(textField_3);
		
		JLabel label = new JLabel("Phone No.:");
		label.setBounds(18, 56, 77, 14);
		panel.add(label);
		
		//Find person button
		JButton btnFind = new JButton("Find");
		btnFind.setBounds(182, 86, 65, 23);
		btnFind.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				findPersonByPhone();
			}
			
		});
		panel.add(btnFind);
		
		JLabel lblFindPerson = new JLabel("Find person");
		lblFindPerson.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblFindPerson.setBounds(93, 33, 154, 14);
		panel.add(lblFindPerson);
		
		JLabel lblPersonInfo = new JLabel("Person info");
		lblPersonInfo.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblPersonInfo.setBounds(93, 136, 154, 14);
		panel.add(lblPersonInfo);
		
		label_2 = new JLabel("");
		label_2.setBounds(10, 11, 237, 14);
		panel.add(label_2);
		
		JButton btnUpdate_2 = new JButton("Update");
		btnUpdate_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateOwnerInfo();
			}
		});
		btnUpdate_2.setBounds(158, 269, 89, 25);
		panel.add(btnUpdate_2);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_1.setBounds(294, 11, 320, 380);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		textField_4 = new JTextField();
		textField_4.setBounds(165, 30, 112, 24);
		panel_1.add(textField_4);
		textField_4.setColumns(10);
		
		JLabel lblName = new JLabel("Sort:");
		lblName.setBounds(30, 37, 139, 14);
		panel_1.add(lblName);
		
		JLabel lblLocation = new JLabel("Life Span:");
		lblLocation.setBounds(30, 72, 139, 14);
		panel_1.add(lblLocation);
		
		JLabel lblNewLabel_1 = new JLabel("Cost per Square Meter:");
		lblNewLabel_1.setBounds(30, 102, 139, 14);
		panel_1.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("Revenue:");
		lblNewLabel_2.setBounds(30, 202, 139, 14);
		panel_1.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Price:");
		lblNewLabel_3.setBounds(30, 237, 139, 14);
		panel_1.add(lblNewLabel_3);
		
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		textField_5.setBounds(165, 62, 112, 24);
		panel_1.add(textField_5);
		
		textField_6 = new JTextField();
		textField_6.setColumns(10);
		textField_6.setBounds(165, 97, 112, 24);
		panel_1.add(textField_6);
		
		textField_7 = new JTextField();
		textField_7.setColumns(10);
		textField_7.setBounds(165, 197, 112, 24);
		panel_1.add(textField_7);
		
		textField_8 = new JTextField();
		textField_8.setColumns(10);
		textField_8.setBounds(165, 232, 112, 24);
		panel_1.add(textField_8);
		
		JButton btnCreate_1 = new JButton("Create");
		btnCreate_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createTree();
			}
		});
		btnCreate_1.setBounds(205, 267, 93, 23);
		panel_1.add(btnCreate_1);
		
		JButton btnClear_1 = new JButton("Delete");
		btnClear_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnClear_1.setBounds(10, 267, 82, 23);
		panel_1.add(btnClear_1);
		
		JButton btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				updateTree();
			}
		});
		btnUpdate.setBounds(102, 267, 93, 23);
		panel_1.add(btnUpdate);
		
		JLabel lblToCreateNew = new JLabel("To create new Tree enter those 3 data:");
		lblToCreateNew.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblToCreateNew.setBounds(30, 11, 247, 14);
		panel_1.add(lblToCreateNew);
		
		JLabel lblAndOwnerOf = new JLabel("And the owner of the trees phone no.:");
		lblAndOwnerOf.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblAndOwnerOf.setBounds(30, 127, 247, 14);
		panel_1.add(lblAndOwnerOf);
		
		textField_9 = new JTextField();
		textField_9.setBounds(165, 152, 112, 24);
		panel_1.add(textField_9);
		textField_9.setColumns(10);
		
		JLabel lblPhoneNo = new JLabel("Phone No.:");
		lblPhoneNo.setBounds(30, 157, 139, 14);
		panel_1.add(lblPhoneNo);
		
		JLabel lblKeyNumbers = new JLabel("Key numbers:");
		lblKeyNumbers.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblKeyNumbers.setBounds(30, 182, 247, 14);
		panel_1.add(lblKeyNumbers);
		
		
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_2.setBounds(624, 11, 356, 380);
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		textField_10 = new JTextField();
		textField_10.setColumns(10);
		textField_10.setBounds(198, 33, 112, 24);
		panel_2.add(textField_10);
		
		textField_11 = new JTextField();
		textField_11.setColumns(10);
		textField_11.setBounds(198, 68, 112, 24);
		panel_2.add(textField_11);
		
		textField_12 = new JTextField();
		textField_12.setColumns(10);
		textField_12.setBounds(198, 103, 112, 24);
		panel_2.add(textField_12);
		
		textField_13 = new JTextField();
		textField_13.setColumns(10);
		textField_13.setBounds(198, 138, 112, 24);
		panel_2.add(textField_13);
		
		textField_14 = new JTextField();
		textField_14.setColumns(10);
		textField_14.setBounds(198, 173, 112, 24);
		panel_2.add(textField_14);
		
		JLabel lblName_1 = new JLabel("Name:");
		lblName_1.setBounds(33, 38, 165, 14);
		panel_2.add(lblName_1);
		
		JLabel lblLocation_1 = new JLabel("Location:");
		lblLocation_1.setBounds(33, 73, 165, 14);
		panel_2.add(lblLocation_1);
		
		JLabel lblSquareMeter = new JLabel("Square Meter:");
		lblSquareMeter.setBounds(33, 108, 165, 14);
		panel_2.add(lblSquareMeter);
		
		JLabel lblNatureOfTrees = new JLabel("Nature of Trees:");
		lblNatureOfTrees.setBounds(33, 143, 165, 14);
		panel_2.add(lblNatureOfTrees);
		
		JLabel lblPrice = new JLabel("Price:");
		lblPrice.setBounds(33, 178, 165, 14);
		panel_2.add(lblPrice);
		
		JLabel lblCreateForestBy = new JLabel("Create Forest by enterings those 5 data:");
		lblCreateForestBy.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblCreateForestBy.setBounds(33, 13, 277, 14);
		panel_2.add(lblCreateForestBy);
		
		JLabel lblEnterThePhone = new JLabel("Enter the Phone No. of the owner to the Forest:");
		lblEnterThePhone.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblEnterThePhone.setBounds(33, 219, 277, 14);
		panel_2.add(lblEnterThePhone);
		
		textField_15 = new JTextField();
		textField_15.setColumns(10);
		textField_15.setBounds(198, 244, 112, 24);
		panel_2.add(textField_15);
		
		JLabel lblPhoneNo_1 = new JLabel("Phone No.:");
		lblPhoneNo_1.setBounds(33, 249, 165, 14);
		panel_2.add(lblPhoneNo_1);
		
		JButton btnCreate_2 = new JButton("Create");
		btnCreate_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createForest();
			}
		});
		btnCreate_2.setBounds(213, 304, 97, 23);
		panel_2.add(btnCreate_2);
		
		JButton btnUpdate_1 = new JButton("Update");
		btnUpdate_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateForest();
			}
		});
		btnUpdate_1.setBounds(109, 304, 89, 23);
		panel_2.add(btnUpdate_1);
		
		JButton btnDelete_1 = new JButton("Delete");
		btnDelete_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deleteForest();
			}
		});
		btnDelete_1.setBounds(109, 338, 89, 23);
		panel_2.add(btnDelete_1);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_3.setBounds(10, 402, 274, 244);
		contentPane.add(panel_3);
		panel_3.setLayout(null);
		
		model = new DefaultListModel<String>();
		list = new JList(model);
		//Create listener for the list
		list.addListSelectionListener(new ListSelectionListener(){
			@Override
			public void valueChanged(ListSelectionEvent listener) {
				if(listener.getValueIsAdjusting()){
					findTrees();
				}
			}
		});
		list.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		list.setBounds(10, 46, 254, 180);
		panel_3.add(list);
		
		JLabel lblForests = new JLabel("Forests");
		lblForests.setHorizontalAlignment(SwingConstants.CENTER);
		lblForests.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblForests.setBounds(10, 11, 254, 37);
		panel_3.add(lblForests);
		
		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_4.setBounds(294, 402, 320, 244);
		contentPane.add(panel_4);
		panel_4.setLayout(null);
		
		model_2 = new DefaultListModel<String>();
		list_2 = new JList(model_2);
		list_2.addListSelectionListener(new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent listener){
				if(listener.getValueIsAdjusting()){
					getTreeInfo();
				}
			}
		});
		list_2.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		list_2.setBounds(10, 48, 300, 177);
		panel_4.add(list_2);
		
		JLabel lblTrees = new JLabel("Trees");
		lblTrees.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblTrees.setHorizontalAlignment(SwingConstants.CENTER);
		lblTrees.setBounds(10, 11, 300, 37);
		panel_4.add(lblTrees);
	}
	
	//Find person by phone method
	public void findPersonByPhone(){
		if(textField_3.getText() != null || textField_3.toString() != ""){
			try{
				owner = ctr.findOwnerByPhone(textField_3.getText());
				if(owner.getPhoneNumber() != null){
					textField.setText(owner.getOwnerName());
					textField_1.setText(owner.getAddress());
					textField_2.setText(owner.getPhoneNumber());
					label_2.setText("Owner found");
					
					//Test delete after test
					//Forest skov = new Forest("Skov", "Abevej", 10, "tree", 10);
					//owner.addForest(skov);
					//label_2.setText("Forest added");
					
					//Get all Forest objects in the owner object.
					fList = owner.getAllForests();
					
					for(int i = 0; i < fList.size(); i++){
						model.addElement(fList.get(i).getName());
					}					
				}
			}catch(Exception e){
				label_2.setText("Owner not found");
				System.out.println(e.getMessage());
			}
		}
	}
	
	//Find Trees in a forest object
	public void findTrees(){
		Tree tree1 = new Tree("Birk", "long", 200 );
		Tree tree2 = new Tree("Oak", "Long", 400);
		
		int i = list.getSelectedIndex();
		f = fList.get(i);
		textField_10.setText(f.getName());
		textField_11.setText(f.getLocation());
		textField_12.setText(Float.toString(f.getSquareMeter()));
		textField_13.setText(f.getNatureOfTrees());
		textField_14.setText(Float.toString(f.getPrice()));
		
		f.addTree(tree1);
		f.addTree(tree2);
		for(int index = 0; index < f.getAllTrees().size(); index++){
			model_2.addElement(f.getAllTrees().get(index).getSort().toString());
		}
	}
	/**
	 * Get tree info.
	 * This method converts all floats to Strings
	 * */
	public void getTreeInfo(){
		int i = list_2.getSelectedIndex();
		Tree t = f.getAllTrees().get(i);
		String cpsk = Float.toString(t.getCostPerSquareKilometer());
		t.setRevenuePerSquareKilometer(f);
		t.setProfit(f);
		String rpsk = Float.toString(t.getRevenuePerSquareKilometer());
		String profit = Float.toString(t.getProfit());
		textField_4.setText(t.getSort());
		textField_5.setText(t.getLifespan());
		textField_6.setText(cpsk);
		textField_7.setText(rpsk);
		textField_8.setText(profit);
	}
	/**
	 * This method updates the tree object with the textfield stats
	 * */
	public void updateTree(){
		int i = list_2.getSelectedIndex();
		Tree t = f.getAllTrees().get(i);
		t.setSort(textField_4.getText());
		t.setLifespan(textField_5.getText());
		//Convert cpsk to float insted of string
		float cpsk = Float.parseFloat(textField_6.getText());
		t.setCostPerSquareKilometer(cpsk);
	}
	/**
	 * Create new tree and add it to the owner by phone
	 * */
	public void createTree(){
		String sort = textField_4.getText();
		String lSpan = textField_5.getText();
		float cpsm = Float.parseFloat(textField_6.getText());
		Tree trae = new Tree(sort, lSpan, cpsm);
		f.addTree(trae);
		list_2.updateUI();
	}
	
	//Create person
	public void createPerson(){
		if((textField.toString() != "" || textField != null) &&  (textField_1.toString() != "" || textField_1 != null) && (textField_2.toString() != "" || textField_2 != null)){
			String name = textField.getText().toString();
			String address = textField_1.getText().toString();
			String phone = textField_2.getText().toString();
			ctr.createOwner(name, address, phone);
			label_2.setText("");
			label_2.setText("Owner created");
			textField.setText("");
			textField_1.setText("");
			textField_2.setText("");
		}
	}
	
	//Delete person
	public void deletePerson(){
		if(textField_2.getText().toString() != null){
			String phone = textField_2.getText().toString();
			ctr.removeOwnerByPhone(phone);
			label_2.setText("");
			label_2.setText("Owner deleted");
			textField.setText("");
			textField_1.setText("");
			textField_2.setText("");
		}
	}
	/**
	 * Update owner info
	 * */
	public void updateOwnerInfo(){
		if(textField.getText() != null){
			owner.setOwnerName(textField.getText().toString());
			textField.setText("");
		}
		if(textField_1.getText() != null){
			owner.setAddress(textField_1.getText());
			textField_1.setText("");
		}
		if(textField_2.getText() != null){
			owner.setPhoneNumber(textField_2.getText());
			textField_2.setText("");
		}
	}
	//Clear text in person fields
	public void clearPersonTxt(){
		try{
			textField.setText("");
			textField_1.setText("");
			textField_2.setText("");
			label_2.setText("");
			label_2.setText("Fields cleared");
		}catch(Exception e){
			label_2.setText("");
			label_2.setText("Couldn't clear fields");
		}
	}
	/**
	 * Create forest
	 * */
	public void createForest(){
		String name = null;
		String location = null;
		float sqm = 0;
		String nOT = null;
		float price = 0;
		if(textField_10 != null){
			name = textField_10.getText();
		}
		if(textField_11 != null){
			location = textField_11.getText();
		}
		if(textField_12 != null){
			sqm = Float.parseFloat(textField_12.getText());
		}
		if(textField_13 != null){
			nOT = textField_13.getText();
		}
		if(textField_14 != null){
			price = Float.parseFloat(textField_14.getText());
		}
		Owner o = ctr.findOwnerByPhone(textField_15.getText());
		Forest fore = new Forest(name, location, sqm, nOT, price);
		o.addForest(fore);
		//Reset all forest textfields
		textField_10.setText("");
		textField_11.setText("");
		textField_12.setText("");
		textField_13.setText("");
		textField_14.setText("");
	}
	/**
	 * Update forest
	 * */
	public void updateForest(){
		String name = null;
		String location = null;
		float sqm = 0;
		String nOT = null;
		float price = 0;
		if(textField_10 != null){
			name = textField_10.getText();
		}
		if(textField_11 != null){
			location = textField_11.getText();
		}
		if(textField_12 != null){
			sqm = Float.parseFloat(textField_12.getText());
		}
		if(textField_13 != null){
			nOT = textField_13.getText();
		}
		if(textField_14 != null){
			price = Float.parseFloat(textField_14.getText());
		}
		f.setName(name);
		f.setLocation(location);
		f.setSquareMeter(sqm);
		f.setNatureOfTrees(nOT);
		f.setPrice(price);
		textField_10.setText("");
		textField_11.setText("");
		textField_12.setText("");
		textField_13.setText("");
		textField_14.setText("");
	}
	/**
	 * Delete Forest
	 * */
	public void deleteForest(){
		String name = null;
		if(textField_10 != null){
			name = textField_10.getText();
		}
		owner.removeForest(name);
		textField_10.setText("");
		textField_11.setText("");
		textField_12.setText("");
		textField_13.setText("");
		textField_14.setText("");
	}
}
